package com.hzcx.app.simplechat.ui.setting;

import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.hzcx.app.simplechat.R;
import com.hzcx.app.simplechat.base.BaseActivity;
import com.hzcx.app.simplechat.ui.group.LiveData.GroupInfoBeanLiveData;
import com.hzcx.app.simplechat.ui.group.bean.GroupInfoBean;
import com.hzcx.app.simplechat.ui.setting.adapter.ImgAdapter;
import com.hzcx.app.simplechat.ui.setting.contract.FeedBackContract;
import com.hzcx.app.simplechat.ui.setting.presenter.FeedBackPresenter;
import com.hzcx.app.simplechat.util.UserInfoUtil;
import com.hzcx.app.simplechat.util.app.AppHelper;
import com.hzcx.app.simplechat.util.empty.EmptyUtils;
import com.hzcx.app.simplechat.util.photo.PhotoUtil;
import com.luck.picture.lib.config.PictureConfig;
import com.luck.picture.lib.config.PictureMimeType;
import com.luck.picture.lib.entity.LocalMedia;
import com.luck.picture.lib.listener.OnResultCallbackListener;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 意见反馈
 */
public class FeedBackActivity extends BaseActivity<FeedBackPresenter> implements BaseQuickAdapter.OnItemClickListener, OnResultCallbackListener<LocalMedia>,
        BaseQuickAdapter.OnItemChildClickListener, FeedBackContract.View {

    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.iv_back)
    ImageView ivBack;
    @BindView(R.id.iv_title_right)
    ImageView ivTitleRight;
    @BindView(R.id.tv_title_right)
    TextView tvTitleRight;
    @BindView(R.id.et_content)
    EditText etContent;
    @BindView(R.id.tv_num)
    TextView tvNum;
    @BindView(R.id.view_line)
    View viewLine;
    @BindView(R.id.tv_img_tips)
    TextView tvImgTips;
    @BindView(R.id.rv_img)
    RecyclerView rvImg;
    @BindView(R.id.view_bg)
    View viewBg;
    @BindView(R.id.feedback_phone_row)
    LinearLayout feedbackPhoneRow;
    @BindView(R.id.et_phone)
    EditText etPhone;
    private List<String> imgList;
    private ImgAdapter imgAdapter;
    private List<LocalMedia> seleteImgList;

    // groupInfoBean 空: 用户反馈    不为空: 群投诉
//    public static String FeedBackActivityGroupInfoBeanKey = "FeedBackActivityGroupInfoBeanKey";
//    private GroupInfoBean groupInfoBean;

    public static String GroupInfoBeanKeyGroupID = "QrCodeActivity_GroupInfoBeanKeyGroupID";
    GroupInfoBean groupInfoBean;
    private String groupID;

    public static String FeedBackActivityReasonKey = "FeedBackActivityReasonKey";
    private String reasonInfo;

    @Override
    public int getLayout() {
        return R.layout.activity_feedback;
    }

    @Override
    public void initPresenter() {
        mPresenter = new FeedBackPresenter();
    }

    @Override
    public void initView() {
        tvTitleRight.setText("提交");

//        groupInfoBean = (GroupInfoBean)getIntent().getSerializableExtra(FeedBackActivityGroupInfoBeanKey);
        groupID = getIntent().getStringExtra(GroupInfoBeanKeyGroupID);
        groupInfoBean = GroupInfoBeanLiveData.getGroupInfoBean(groupID);

        reasonInfo = getIntent().getStringExtra(FeedBackActivityReasonKey);
        if (groupInfoBean != null) {
            feedbackPhoneRow.setVisibility(View.GONE);
            tvTitle.setText("群投诉");
            etContent.setHint("输入你要投诉的内容");
        } else {
            feedbackPhoneRow.setVisibility(View.VISIBLE);
            tvTitle.setText("意见反馈");
        }

        tvNum.setText(etContent.getText().toString().length() + "/300");
        etContent.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                tvNum.setText(etContent.getText().toString().length() + "/300");
            }

            @Override
            public void afterTextChanged(Editable editable) {

            }
        });

        seleteImgList = new ArrayList<>();
        imgList = new ArrayList<>();
        imgAdapter = new ImgAdapter(R.layout.rv_item_img, imgList);
        rvImg.setLayoutManager(new GridLayoutManager(this, 3));
        rvImg.setAdapter(imgAdapter);

        imgAdapter.setOnItemClickListener(this);
        imgAdapter.setOnItemChildClickListener(this);
    }

    @Override
    public void initData() {

        // 添加初始值
        imgList.add("");
        imgAdapter.notifyDataSetChanged();

        etPhone.setText(AppHelper.hidePhone(UserInfoUtil.getUserPhone()));

    }

    @OnClick({R.id.tv_title_right})
    public void viewOnClick(View view) {
        switch (view.getId()) {
            case R.id.tv_title_right:
                if (EmptyUtils.isEmpty(etContent.getText().toString()) && seleteImgList.size() <= 0) {
                    showError("请输入内容或选择图片");
                    return;
                }
                showLoading();
                mPresenter.upLoadImg(this, imgList);
                break;
        }
    }

    /**
     * 图片列表点击
     *
     * @param adapter
     * @param view
     * @param position
     */
    @Override
    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
        if (EmptyUtils.isEmpty(imgList.get(position))) {
            PhotoUtil.showPhoto(this, PictureMimeType.ofImage(), PictureConfig.MULTIPLE, true, false, seleteImgList, this);
        } else {
            PhotoUtil.openPhoto(this, seleteImgList, position);
        }
    }

    /**
     * 图片选取返回
     *
     * @param result
     */
    @Override
    public void onResult(List<LocalMedia> result) {
        seleteImgList.clear();
        seleteImgList.addAll(result);
        imgList.clear();
        for (LocalMedia media : result) {
            imgList.add(PhotoUtil.getPath(media));
        }
        if (imgList.size() < 9) {
            imgList.add(imgList.size(), "");
        }
        imgAdapter.notifyDataSetChanged();
    }

    @Override
    public void onCancel() {

    }

    /**
     * 子控件点击
     *
     * @param adapter
     * @param view
     * @param position
     */
    @Override
    public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
        switch (view.getId()) {
            case R.id.iv_delete:
                deleteImg(position);
                break;
        }
    }

    /**
     * 删除图片
     *
     * @param pos
     */
    private void deleteImg(int pos) {
        seleteImgList.remove(pos);
        imgList.remove(pos);
        if (EmptyUtils.isNotEmpty(imgList.get(imgList.size() - 1))) {
            imgList.add(imgList.size(), "");
        }
        imgAdapter.notifyDataSetChanged();
    }

    /**
     * 提交成功
     */
    @Override
    public void putSuccess() {
        showError("提交成功");
        finish();
    }

    /**
     * 图片上传成功
     *
     * @param url
     */
    @Override
    public void upLoadSuccess(String url) {
        if (groupInfoBean != null) {
            mPresenter.complaintGroup(this, groupInfoBean.getGroup_id(), reasonInfo, url, etContent.getText().toString());
        } else {
            mPresenter.putFeedback(this, etContent.getText().toString(), url, etPhone.getText().toString());
        }
    }
}
